package com.zhongketech.sunflower.control;

import java.lang.reflect.Method;
import java.util.List;

import com.android.internal.telephony.ITelephony;
import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.transport.model.SpecialNums;
import com.zhongketech.sunflower.utils.Utilities;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class Phone extends BroadcastReceiver{
	private static final String TAG = "Phone";

	private static boolean incomingFlag = false;

	private static String incoming_number = null;

	@Override
	public void onReceive(Context context, Intent intent)
	{		
		//如果是拨打电话
		if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
		{                        
			incomingFlag = false;
			String phoneNumber = Utilities.replace(intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));        
			LogUtil.i(TAG, "call OUT:"+phoneNumber);

			if((!PhoneManager.getInstance().isExist(phoneNumber)) || PhoneManager.getInstance().isBlack(phoneNumber))
			{
				Toast.makeText(context, "禁止拨打此电话",
						Toast.LENGTH_SHORT).show();
				setResultData(null);	
			}
		}
		else
		{                        
			//如果是来电
			TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        

			switch (tm.getCallState()) 
			{
			case TelephonyManager.CALL_STATE_RINGING:
				incomingFlag = true;//标识当前是来电
				incoming_number = Utilities.replace(intent.getStringExtra("incoming_number"));
				LogUtil.i(TAG, "RINGING :"+ incoming_number);

				if((!PhoneManager.getInstance().isExist(incoming_number)) || PhoneManager.getInstance().isBlack(incoming_number))
				{
					Toast.makeText(context, "禁止接听该电话",
							Toast.LENGTH_SHORT).show();
					endCall(context);
				}

				break;
			case TelephonyManager.CALL_STATE_OFFHOOK:                                
				if(incomingFlag)
				{
					LogUtil.i(TAG, "incoming ACCEPT :"+ incoming_number);
				}
				break;
			case TelephonyManager.CALL_STATE_IDLE:                                
				if(incomingFlag)
				{
					LogUtil.i(TAG, "incoming IDLE");                                
				}
				break;
			} 
		}
	}

	private static void endCall(Context cx) 
	{ 
		TelephonyManager telMag = (TelephonyManager) cx.getSystemService(Context.TELEPHONY_SERVICE);
		Class<TelephonyManager> c = TelephonyManager.class;
		Method mthEndCall = null;

		try 
		{
			mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
			mthEndCall.setAccessible(true);
			ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,(Object[]) null);
			iTel.endCall();
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
	}
}
